package oa.hleast.xswl.controller.admin.studies;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.xswl.pojo.domain.place.CmsPlaceInfo;
import oa.hleast.xswl.pojo.domain.studies.*;
import oa.hleast.xswl.pojo.vo.studies.ResearchActivitesPathVo;
import oa.hleast.xswl.service.place.IPlaceInfoService;
import oa.hleast.xswl.service.studies.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "【系统管理】团党组织负责人管理")
@RestController("AdminStudiesOrganizationController")
@RequestMapping("/api.admin/v1/organization")
@Slf4j
@AllArgsConstructor
public class AdminStudiesOrganizationController {

    @Autowired
    private CaucusOrganizationService caucusOrganizationService;

    @Autowired
    private CaucusOrganizationApplicationService caucusOrganizationApplicationService;

    @Autowired
    private CaucusOrganizationMemberService caucusOrganizationMemberService;

    @Autowired
    private ResearchActivitiesService researchActivitiesService;

    @Autowired
    private ResearchActivitiesPathService researchActivitiesPathService;

    @Autowired
    private IPlaceInfoService placeInfoService;

    /**
     * 党团组织分页查询
     * @param queryMode
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "党团组织列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("/organization")
    public Result Organization(
            String queryMode,
            Integer page,
            Integer limit
    ) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                LambdaQueryWrapper<CaucusOrganization> queryWrapper = new LambdaQueryWrapper<CaucusOrganization>()
                        .orderByDesc(CaucusOrganization::getGmtModified)
                        .orderByDesc(CaucusOrganization::getGmtCreate);
                Page<CaucusOrganization> result = caucusOrganizationService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(caucusOrganizationService.list(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @Transactional
    @ApiOperation(value = "新增团党组织（提交审核）", httpMethod = "POST")
    @PostMapping("/addOrganization")
    public Result addOrganization(@RequestBody CaucusOrganizationApplication caucusOrganizationApplication){
        boolean status= caucusOrganizationApplicationService.addQuestionnaire(caucusOrganizationApplication);
        return Result.judge(status);
    }

    @Transactional
    @ApiOperation(value = "修改团党组织", httpMethod = "POST")
    @PostMapping("/updateOrganization")
    public Result updateOrganization(@RequestBody CaucusOrganization caucusOrganization){
        boolean status= caucusOrganizationApplicationService.updateOrganization(caucusOrganization);
        return Result.judge(status);
    }

    @Transactional
    @ApiOperation(value = "删除团党组织", httpMethod = "POST")
    @PostMapping("/deleteOrganization")
    public Result deleteOrganization(@RequestBody CaucusOrganization caucusOrganization){
        boolean status= caucusOrganizationApplicationService.deleteOrganization(caucusOrganization);
        return Result.judge(status);
    }

    /**
     * 团党组织成员审核通过列表分页
     * @param queryMode
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "党团组织成员列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "caucusOrganizationId", value = "所属团党组织Id", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("/organizationMember")
    public Result organizationMember(
            String queryMode,
            Integer page,
            Integer limit,
            Integer caucusOrganizationId
    ) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                LambdaQueryWrapper<CaucusOrganizationMember> queryWrapper = new LambdaQueryWrapper<CaucusOrganizationMember>()
                        .orderByDesc(CaucusOrganizationMember::getGmtModified)
                        .orderByDesc(CaucusOrganizationMember::getGmtCreate);
                Page<CaucusOrganizationMember> result = caucusOrganizationMemberService.page(new Page<>(page, limit), queryWrapper);
                List<CaucusOrganizationMember> caucusOrganizationMemberList = caucusOrganizationMemberService.list(new LambdaQueryWrapper<CaucusOrganizationMember>()
                        .eq(CaucusOrganizationMember::getCaucusOrganizationId, caucusOrganizationId));
                return Result.success(caucusOrganizationMemberList, result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    /**
     * 手动添加团党组织成员
     * @param caucusOrganizationMember
     * @return
     */
    @Transactional
    @ApiOperation(value = "手动添加团党组织成员", httpMethod = "POST")
    @PostMapping("/manuallyAddOrganizationMember")
    public Result manuallyAddOrganizationMember(@RequestBody CaucusOrganizationMember caucusOrganizationMember){
        boolean status= caucusOrganizationApplicationService.manuallyAddOrganizationMember(caucusOrganizationMember);
        return Result.judge(status);
    }

    /**
     * 修改团组织成员
     * @param caucusOrganizationMember
     * @return
     */
    @Transactional
    @ApiOperation(value = "修改团党组织成员（包括审核状态，修改信息）", httpMethod = "POST")
    @PostMapping("/updateOrganizationMember")
    public Result updateOrganizationMember(@RequestBody CaucusOrganizationMember caucusOrganizationMember){
        boolean status= caucusOrganizationApplicationService.updateOrganizationMember(caucusOrganizationMember);
        return Result.judge(status);
    }

    /**
     * 批量删除团党组织成员
     * @param ids
     * @return
     */
    @Transactional
    @ApiOperation(value = "批量删除团党组织成员", httpMethod = "GET")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @GetMapping("/deleteOrganizationMember/{ids}")
    public Result deleteOrganizationMember(@PathVariable("ids") String ids){
        boolean status= caucusOrganizationApplicationService.deleteOrganizationMember(ids);
        return Result.judge(status);
    }

    /**
     * 查询研学路线分页
     * @param queryMode
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "研学线路列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("/studies")
    public Result studies(
            String queryMode,
            Integer page,
            Integer limit
    ) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                LambdaQueryWrapper<ResearchActivitiesPath> queryWrapper = new LambdaQueryWrapper<ResearchActivitiesPath>()
                        .orderByDesc(ResearchActivitiesPath::getGmtModified)
                        .orderByDesc(ResearchActivitiesPath::getGmtCreate);
                Page<ResearchActivitiesPath> result = researchActivitiesPathService.page(new Page<>(page, limit), queryWrapper);

                List<ResearchActivitesPathVo> researchActivitesPathVoList=researchActivitiesPathService.findAll();

                return Result.success(researchActivitesPathVoList, result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @ApiOperation(value = "研学点列表分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("/studiesPoint")
    public Result studiesPoint(
            String queryMode,
            Integer page,
            Integer limit
    ) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            case PAGE:
                LambdaQueryWrapper<CmsPlaceInfo> queryWrapper = new LambdaQueryWrapper<CmsPlaceInfo>()
                        .orderByDesc(CmsPlaceInfo::getGmtModified)
                        .orderByDesc(CmsPlaceInfo::getGmtCreate);
                Page<CmsPlaceInfo> result =placeInfoService.page(new Page<>(page, limit), queryWrapper);

                return Result.success(placeInfoService.list(), result.getTotal());
            default:
                return Result.failed(ResultCode.QUERY_MODE_IS_NULL);
        }
    }

    @Transactional
    @ApiOperation(value = "在原有线路添加点(研学点位申请)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "researchActivitiesPathId", paramType = "query", dataType = "Integer", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "ids", value = "点位集合", paramType = "query", dataType = "String", dataTypeClass = Integer.class)
    })
    @GetMapping("/addStudiesPoint")
    public Result addStudiesPoint(
            Integer researchActivitiesPathId,
            String ids
    ){
        boolean status= researchActivitiesService.addStudiesPoint(researchActivitiesPathId,ids);
        return Result.judge(status);
    }
}
